// goods/content/index.js
import Toast from '../../ui/toast/index'
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:0,
        pic_url:app.base_url,
        navigation: { type: 'fraction' },
        paginationPosition: 'bottom-right',
        pic_list:[app.base_url+'wximage/banner.png',app.base_url+'wximage/banner2.png'],
        popshow:false,
        pop_list:[],//显示内容
        content:{},
        popshow_goods:false,
        value:0,
        goods_attr:[],//商品选择的属性组
        goods_attr_info:{},//选择的商品信息
        goods_attr_index:[],//属性选中状态
        car_num:1,//加入购物车数量
        attr_id:-1,//属性index
        car_goods_num:0,//购物车总数量
        yhq_popshow:false,
        yhq_list:[],
        rate_list:[],
    },
    //选择
    onChange(e){
        this.setData({
            value:e.detail.value,
        });
    },

    //获取评价
    get_rate_list(){
        app.http.request("index.php?r=api/content/ratenumlist",{param:{id:this.data.id,num:3}}).then((res)=>{
            console.log(res);
            if(res.data.result==1){
                this.setData({
                    rate_list:res.data.data,
                });
            }
        });
    },

    //有用
    click_use(e){
        let id = e.currentTarget.dataset.id;
        app.login.request("index.php?r=api/content/addrateuse",{id:id}).then((res)=>{
            if(res.data.result==1){
                Toast({
                    selector:"#toast",
                    message:res.data.data,
                    context:this,
                });
            }else{
                Toast({
                    selector:'#toast',
                    message:res.data.data,
                    context:this,
                });
            }
        });
    },

    //跳转评价
    to_rate(e){
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/goods/rate/index?id='+id,
        })
    },

    //显示优惠券
    change_yhq_pop(){
        this.setData({
            yhq_popshow:true,
        });
        this.get_yhq_list();
    },

    //获取优惠券
    get_yhq_list(){
        app.http.request("index.php?r=api/content/yhq",{}).then((res)=>{
            if(res.data.result==1){
                this.setData({
                    yhq_list:res.data.data,
                });
            }
        });
    },

    //领取优惠券
    click_yhq(e){
        let id = e.currentTarget.dataset.id;
        app.login.request("index.php?r=api/content/getyhq",{id:id}).then((res)=>{
            if(res.data.result){
                Toast({
                    context:this,
                    selector:"#toast",
                    message:"领取成功"
                });
                this.get_yhq_list();
            }else{
                Toast({
                    context:this,
                    selector:"#toast",
                    message:res.data.data,
                });
            }
        });
    },

    close_yhq_pop(){
        this.setData({
            yhq_popshow:false,
        });
    },

    //显示pop
    change_pop(e){
        let type = e.currentTarget.dataset.type;
        let list=[];
        switch(type){
            case "discount":
                let content = this.data.content.discount_info.content;
                for(let i=0;i<content.length;i++){
                    let res = "满"+content[i].count+"件减"+content[i].reduce+"元";
                    list.push(res);
                }
                break;
            case "new":
                let res = "限购"+this.data.content.new_info.limit+"件";
                list.push(res);
                break;    
        }
        this.setData({
            popshow:true,
            pop_list:list,
        });
    },
    //关闭pop
    close_pop(){
        this.setData({
            popshow:false,
            popshow_goods:false,
        });
    },

    //显示商品添加
    show_goods_pop(){
        this.setData({
            popshow_goods:true,
        });
    },

    //获取内容
    get_content(){
        app.http.request("index.php?r=api/content/one",{id:this.data.id,address:wx.getStorageSync('address')}).then((res)=>{
            console.log(res);
            if(res.data.result){
                this.data.content = res.data.data;
                this.data.pic_list = this.data.content.pics;
                // let price_num = this.data.content.goods_price.split(".");
                // this.data.content.goods_price = '<text class="num_l">'+price_num[0]+'</text><text class="num_r">'+price_num[1]+'</text>';
                this.data.goods_attr_info.thumb_pic = this.data.content.thumb_pic;
                    this.data.goods_attr_info.goods_num = this.data.content.goods_num;
                    this.data.goods_attr_info.goods_price = this.data.content.goods_price;
                    this.data.goods_attr_info.old_price = this.data.content.goods_price;
                if(this.data.content.specs_value.length>0){//属性商品
                    this.data.goods_attr_info.is_num=0;
                }else{//非属性商品
                    this.data.goods_attr_info.is_num=1;
                }
                
                //初始化状态选择
                let specs = this.data.content.specs;
                for(let i=0;i<specs.length;i++){
                    this.data.goods_attr_index[i]={index:-1};
                }
                this.setData({
                    content:this.data.content,
                    pic_list:this.data.pic_list,
                    goods_attr_info:this.data.goods_attr_info,
                });
            }else{
                wx.showModal({
                  title: '提示',
                  content: res.data.data,
                  showCancel:false,
                  complete: (res) => {
                    if (res.confirm) {
                    }
                  }
                })
            }
        });
    },

    //属性操作
    change_attr(e){
        let type = e.currentTarget.dataset.type;
        let index = e.currentTarget.dataset.index;
        let index2=e.currentTarget.dataset.index2;
        this.data.goods_attr[index]=type;
        this.data.goods_attr_index[index].index=index2;
        this.setData({
            goods_attr_index:this.data.goods_attr_index,
        });

        //获取相关内容
        app.http.request("index.php?r=api/content/attrgoods",{attr:this.data.goods_attr,id:this.data.content.id}).then((res)=>{
            console.log(res);
            if(res.data.result==1){
                let data = res.data.data;
                //设置库存
                if(this.data.car_num>data.goods_num){
                    this.data.car_num=data.goods_num;
                }else{
                    this.data.car_num=1;
                }
                this.data.attr_id = res.data.attr_id;
                this.setData({
                    goods_attr_info:res.data.data,
                    car_num:this.data.car_num,
                });
                
            }
            if(res.data.result==0){
                wx.showModal({
                  title: '提示',
                  content: res.data.data,
                  showCancel:false,
                  complete: (res) => {
                    if (res.confirm) {
                      
                    }
                  }
                })
            }
        });
        
    },

    //检查库存
    check_goods_num(e){
        let num=e.detail.value;
        if(this.data.content.specs_value.length<=0){
            if(num>this.data.content.goods_num){
                Toast({
                    context:this,
                    selector:'#toast',
                    message:'超出最大库存数量',
                });
                this.setData({
                    car_num:this.data.goods_attr_info.goods_num,
                });
                return;
            }
        }else if(num>this.data.goods_attr_info.goods_num){
            Toast({
                context:this,
                selector:'#toast',
                message:'超出最大库存数量',
            });
            this.setData({
                car_num:this.data.goods_attr_info.goods_num,
            });
            return;
        }
        this.setData({
            car_num:num,
        });
    },

    //添加购物车
    add_car(){
        let p ={};
        p.id=this.data.content.id;
        if(this.data.content.specs_value.length>0){
            p.attr_id=this.data.attr_id;
            if(p.attr_id<0){
                Toast({
                    context:this,
                    message:"请选择商品属性",
                    selector:"#toast",
                });
                return;
            }
        }else{
            p.attr_id=-1;
        }
        
        p.num = this.data.car_num;
        
        if(p.num<=0){
            Toast({
                context:this,
                message:"请选择商品数量",
                selector:"#toast",
            });
            return;
        }
        let is=false;
        let arr = wx.getStorageSync('goods');
        if(arr){
            //判断是否有重复商品
            for(let i=0;i<arr.length;i++){
                if(arr[i].id==p.id&&arr[i].attr_id==p.attr_id){
                    let num=arr[i].num+p.num;
                    if(num>this.data.goods_attr_info.goods_num){//超出库存
                        num=this.data.goods_attr_info.goods_num;
                    }
                    arr[i].num=num;
                    is=true;
                    break;
                }
            }
            if(!is){
                arr.push(p);
            }
        }else{
            arr=[];
            arr.push(p);
        }
        wx.setStorageSync('goods', arr);
        this.refresh_car();
        Toast({
            context:this,
            message:'成功添加购物车',
            selector:"#toast",
        });
        this.setData({
            popshow_goods:false,
        });
    },

    //更新购物车数量
    refresh_car(){
        let goods = wx.getStorageSync('goods');
        let num=0;
        if(goods){
            num=goods.length;
        }
        this.setData({
            car_goods_num:num,
        });
    },

    //跳转购物车
    to_car(){
        wx.navigateTo({
            url: '/car/index/index',
        })
    },

    //显示客服
    show_kf(){
        wx.showLoading({
          title: '加载中',
        })
    },

    //隐藏客服
    hide_kf(){
        wx.hideLoading();
    },

    //点击
    change_hits(){
        app.http.request("index.php?r=api/content/hits",{id:this.data.id}).then(()=>{});
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let id = options.id;
        if(id){
            this.data.id = id;
            this.get_content();
            this.refresh_car();
            this.change_hits();
            this.get_rate_list();
        }
        app.get_name();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.hide_kf();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})